// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: CNZ Setup Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 40 : TYPE_CNZSETUP

// Function declarations
reserve function CNZSetup_Function97
reserve function CNZSetup_Function98
reserve function CNZSetup_Function99
reserve function CNZSetup_Function100
reserve function CNZSetup_Function101
reserve function CNZSetup_Function102

// Static Values

// Tables
public table CNZSetup_table39
	0xE08000, 0xE0A000, 0xC0E000, 0xE0E0E0, 0xA0E000, 0x40E000, 0xC060, 0x40C0C0, 0x80E0, 0x40E0, 0x4000E0, 0x8000C0, 0xE020C0, 0xE00080, 0xE00040, 0xE00000
	0xE04000, 0xE06000
end table

public table CNZSetup_table41
	390, 391, 394, 395, 398, 399, 402, 403, 406, 407, 410, 411, 414, 415, 418, 419
	422, 423, 426, 427, 430, 431, 434, 435, 438, 439, 442, 443, 446, 447, 450, 451
end table

public table CNZSetup_table42
	388, 389, 392, 393, 396, 397, 400, 401, 404, 405, 408, 409, 412, 413, 416, 417
	420, 421, 424, 425, 428, 429, 432, 433, 436, 437, 440, 441, 444, 445, 448, 449
end table

public table CNZSetup_table40
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
	1, 2, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
	4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4
	4, 4, 4, 5, 6, 7, 0, 1, 2, 3, 4, 5, 6, 7, 255
end table

public table CNZSetup_table38
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
	1, 2, 1, 3, 1, 2, 2, 1, 2, 3, 1, 2, 1, 2, 0, 0
	2, 0, 3, 2, 2, 3, 2, 2, 1, 3, 0, 0, 1, 0, 1, 3
end table

private table CNZSetup_table0
	0x480000, 0x2AC0000, 0, 73, 8, 107, 0, 1, 48, 1, 32, 3, 0, 3, 4, 20, 0, 37, 4, 23, 0, 108, 4, 23, 0, 24, 8, 60, 56, 1, 40, 31, 8, 4, 0, 9, 4, 8, 0, 41, 8, 99, 0, 2, 4, 39, 0, 18, 8, 2, 0, 9, 8, 37, 56, 1, 40, 17, 32, 6, 36, 25, 32, 5, 40, 42, 32, 8, 36, 6, 32, 3, 40, 89, 32, 1, 36, 3, 4, 37, 0, 5, 8, 45, 56, 1, 40, 3, 32, 13, 40, 8, 32, 1, 36, 2, 4, 71, 0, 115, 8, 38, 0, 14, 8, 12, 0, 1, 4, 22, 0, 30, 48, 1, 32, 2, 40, 45, 32, 9, 40, 8, 8, 51, 0, 1, 4, 106, 0, 14, 4, 9, 0, 2, 4, 14, 0, 15, 4, 18, 0, 27
end table

private table CNZSetup_table1
	0x480000, 0x2B00000, 0, 52, 8, 105, 56, 1, 32, 3, 0, 8, 4, 20, 0, 12, 8, 10, 56, 1, 40, 41, 8, 7, 0, 6, 4, 11, 0, 3, 48, 1, 32, 4, 40, 31, 8, 73, 56, 1, 40, 65, 8, 29, 0, 20, 4, 7, 0, 2, 8, 36, 56, 1, 40, 6, 8, 11, 0, 2, 48, 1, 32, 4, 40, 5, 8, 5, 56, 1, 40, 8, 8, 6, 0, 46, 8, 114, 0, 3, 4, 51, 0, 28, 8, 27, 0, 9, 4, 9, 0, 179, 8, 38, 0, 12, 8, 17, 0, 1, 4, 4, 0, 15, 4, 9, 0, 1, 8, 15, 0, 1, 4, 15, 0, 8, 4, 18, 52, 1, 36, 5, 4, 7, 52, 1, 36, 4, 4, 5, 48, 1, 32, 4, 0, 3, 48, 1, 32, 4, 40, 3, 56, 1, 40, 4, 56, 1, 32, 4, 40, 2, 8, 1, 56, 1, 40, 3, 56, 1, 40, 4, 8, 1, 48, 1, 32, 3, 48, 1, 32, 4, 48, 1, 32, 4, 48, 1, 32, 3, 0, 1, 48, 1, 32
	3, 48, 1, 32, 4, 0, 1, 48, 1, 32, 3, 48, 1, 32, 4, 48, 1, 32, 4, 48, 1, 32, 3, 0, 1, 48, 1, 32, 3, 48, 1, 32, 6, 48, 1, 32, 1, 48, 1, 32, 6, 48, 1, 32, 3, 48, 1, 32, 2, 40, 4, 56, 1, 40, 2, 56, 1, 40, 1, 32, 6, 0, 1, 48, 1, 32, 2, 48, 1, 32, 6, 48, 1, 32, 3, 48, 1, 32, 5, 0, 1, 48, 1, 32, 2, 48, 1, 32, 5, 0, 1, 48, 1, 32, 2, 48, 1, 32, 5, 0, 1, 48, 1, 32, 3, 48, 1, 32, 5, 0, 1, 48, 1, 32, 3, 48, 1, 32, 5, 48, 1, 32, 1, 40, 3, 56, 1, 40, 4, 56, 1, 40, 1, 32, 3, 48, 1, 32, 5, 48, 1, 32, 5, 48, 1, 32, 5, 0, 1, 48, 1, 32, 2, 48, 1, 32, 4, 0, 2, 48, 1, 32, 2, 48, 1, 32, 4, 48, 1, 32, 4, 48, 1, 32, 5, 36, 1, 4, 1, 52, 1, 36, 1, 52, 1, 36
	7, 4, 1, 52, 1, 36, 2, 32, 1, 48, 1, 32, 7, 0, 1, 52, 1, 36, 3, 4, 23, 52, 1, 32, 1, 40, 2, 56, 1, 40, 7, 8, 1, 56, 1, 40, 2, 56, 1, 40, 1, 32, 4, 48, 1, 32, 5, 48, 1, 32, 3, 0, 2, 48, 1, 32, 2, 48, 1, 32, 6, 0, 2, 48, 2, 32, 7, 0, 1, 48, 1, 32, 1, 48, 1, 32, 6, 48, 1, 32, 3, 48, 1, 32, 3, 48, 1, 32, 5, 48, 1, 32, 5, 0, 1, 48, 1, 32, 2, 48, 1, 32, 6, 0, 2, 48, 2, 32, 4, 40, 3, 8, 3, 48, 1, 32, 1, 48, 1, 36, 6, 4, 2, 52, 1, 36, 1, 52, 1, 36, 7, 4, 1, 52, 1, 36, 3, 52, 1, 36, 3, 4, 10, 0, 9, 8, 10, 0, 33, 8, 5
end table

private table CNZSetup_table2
	0x480000, 0x2AC0000, 0, 58, 8, 133, 56, 1, 40, 12, 8, 13, 56, 1, 40, 250, 8, 32, 56, 1, 40, 5, 8, 6, 0, 9, 4, 5, 0, 15, 4, 4, 0, 8, 4, 8, 0, 5, 4, 13, 0, 25, 8, 11, 0, 3, 4, 23, 0, 4, 8, 29, 48, 1, 36, 8, 4, 1, 0, 32, 4, 27, 52, 1, 36, 2, 32, 1, 0, 19, 8, 14, 0, 10, 4, 8, 52, 1, 36, 18, 32, 2, 40, 46, 32, 1, 36, 20, 32, 1, 40, 14, 42, 43, 10, 14, 58, 1, 42, 5, 10, 2, 8, 8, 0, 11, 4, 18, 52, 1, 36, 72, 37, 17, 5, 2, 4, 34, 0, 12, 4, 3, 0, 6, 4, 102, 52, 1, 36, 3, 4, 184, 52, 1, 36, 24, 4, 7, 52, 1, 36, 10, 37, 14, 36, 23, 37, 11, 33, 2, 41, 2, 9, 10, 57, 1, 41, 11, 9, 16, 57, 1, 41, 39, 40, 2, 8, 12, 56, 1, 40, 5, 32, 10, 40, 47, 32, 1, 
end table

function CNZSetup_Function97
	if object.gravity == GRAVITY_AIR
		object.state = CNZSetup_Function98
		object.value1 = 0
		CallFunction(PlayerObject_HandleAirMovement)
	else
		temp1 = 0
		GetBit(temp0, object.direction, 0)
		if temp0 == 0
			if object.left == 1
				if object.speed > 0
					object.speed -= object.value31
				else
					object.direction ^= FLIP_X
					object.speed = -0x8000
				end if
			else
				if object.angle == 0
					if object.speed <= 0
						object.speed = 0x40000
					end if
				end if
			end if
		else
			if object.right == 1
				if object.speed < 0
					object.speed += object.value31
				else
					object.direction ^= FLIP_X
					object.speed = 0x8000
				end if
			else
				if object.angle == 0
					if object.speed >= 0
						object.speed = -0x40000
					end if
				end if
			end if
		end if
		if object.speed > 0
			object.speed -= object.value29
			Sin256(temp0, object.angle)
			if temp0 > 0
				Sin256(temp0, object.angle)
				temp0 *= 0x5000
			else
				Sin256(temp0, object.angle)
				temp0 *= 0x1E00
			end if
			temp0 >>= 8
			object.speed += temp0
		else
			object.speed += object.value29
			Sin256(temp0, object.angle)
			if temp0 < 0
				Sin256(temp0, object.angle)
				temp0 *= 0x5000
			else
				Sin256(temp0, object.angle)
				temp0 *= 0x1E00
			end if
			temp0 >>= 8
			object.speed += temp0
		end if
		CallFunction(PlayerObject_RollAnimSpd)
		object.animationSpeed = object.value5
		CallFunction(PlayerObject_ResetOnFloor)
	end if
end function


function CNZSetup_Function98
	CallFunction(PlayerObject_AirAcceleration)
	if object.gravity == GRAVITY_AIR
		CallFunction(PlayerObject_HandleAirMovement)
	else
		object.state = CNZSetup_Function97
		CallFunction(PlayerObject_ResetOnFloor)
		object.value14 = 0
	end if
	object.animation = ANI_JUMPING
end function


function CNZSetup_Function99
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("CasinoNight_F.ogg", 0, 0xC488, 0x1F40)
			break
		case 2
			SetMusicTrack("CasinoNight_F.ogg", 0, 0xC488)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("CasinoNight_F.ogg", 0, 0xC488)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function CNZSetup_Function100
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("CasinoNight.ogg", 0, 0xF564, 0x30D4)
			break
		case 2
			SetMusicTrack("CasinoNight.ogg", 0, 0xF564)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("CasinoNight.ogg", 0, 0xF564)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


function CNZSetup_Function101
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			SwapMusicTrack("CasinoNight2_F.ogg", 0, 0x12BAE, 0x1F40)
			break
		case 2
			SetMusicTrack("CasinoNight2_F.ogg", 0, 0x12BAE)
			SwapMusicTrack("Invincibility_F.ogg", 2, 0x78B1, 0x1F40)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("CasinoNight2_F.ogg", 0, 0x12BAE)
			SetMusicTrack("Invincibility_F.ogg", 2, 0x78B1)
			break
		end switch
	else
		stage.musicFlag = 1
	end if
end function


function CNZSetup_Function102
	CheckEqual(object[25].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[25].propertyValue, 2)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, 0)
	temp0 &= checkResult
	if temp0 == 0
		switch music.currentTrack
		case 0
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			SwapMusicTrack("CasinoNight2.ogg", 0, 0x1767C, 0x30D4)
			break
		case 2
			SetMusicTrack("CasinoNight2.ogg", 0, 0x1767C)
			SwapMusicTrack("Invincibility.ogg", 2, 0x9717, 0x30D4)
			break
		case 4
		case 6
		case 7
			SetMusicTrack("CasinoNight2.ogg", 0, 0x1767C)
			SetMusicTrack("Invincibility.ogg", 2, 0x9717)
			break
		end switch
	else
		stage.musicFlag = 2
	end if
end function


event ObjectMain
	object.value5++
	if object.value5 > 7
		tileLayer[1].deformationOffset++
		object.value5 = 0
	end if
	object.value1++
	if object.value1 == 8
		object.value1 = 0
		RotatePalette(0, 165, 167, 1)
		RotatePalette(0, 171, 173, 1)
		RotatePalette(0, 178, 180, 0)
		object.value2++
		object.value2 %= 18
		GetTableValue(temp0, object.value2, CNZSetup_table39)
		SetPaletteEntry(0, 187, temp0)
		object.value3++
		object.value3 %= 18
		GetTableValue(temp0, object.value3, CNZSetup_table39)
		SetPaletteEntry(0, 186, temp0)
		object.value4++
		object.value4 %= 18
		GetTableValue(temp0, object.value4, CNZSetup_table39)
		SetPaletteEntry(0, 185, temp0)
	end if
	if object.animationTimer < 2
		GetTableValue(temp0, object.value6, CNZSetup_table40)
		if temp0 == 255
			object.value6 = 0
			GetTableValue(temp0, object.value6, CNZSetup_table40)
		end if
		temp0 <<= 2
		GetTableValue(temp1, temp0, CNZSetup_table41)
		Copy16x16Tile(760, temp1)
		temp0++
		GetTableValue(temp1, temp0, CNZSetup_table41)
		Copy16x16Tile(761, temp1)
		temp0++
		GetTableValue(temp1, temp0, CNZSetup_table41)
		Copy16x16Tile(762, temp1)
		temp0++
		GetTableValue(temp1, temp0, CNZSetup_table41)
		Copy16x16Tile(763, temp1)
		object.value6++
		GetTableValue(temp0, object.value7, CNZSetup_table40)
		if temp0 == 255
			object.value7 = 0
			GetTableValue(temp0, object.value7, CNZSetup_table40)
		end if
		temp0 <<= 2
		GetTableValue(temp1, temp0, CNZSetup_table42)
		Copy16x16Tile(764, temp1)
		temp0++
		GetTableValue(temp1, temp0, CNZSetup_table42)
		Copy16x16Tile(765, temp1)
		temp0++
		GetTableValue(temp1, temp0, CNZSetup_table42)
		Copy16x16Tile(766, temp1)
		temp0++
		GetTableValue(temp1, temp0, CNZSetup_table42)
		Copy16x16Tile(767, temp1)
		object.value7++
		object.animationTimer = 6
	else
		object.animationTimer--
	end if
	
	if stage.debugMode == 0
		if stage.actNum == 2
			if saveRAM[404] == 0
				temp0 = 0
				foreach (TypeName[Slot Machine], arrayPos0, ALL_ENTITIES)
					if object[arrayPos0].propertyValue != 0
						if object[arrayPos0].value17 == 0
							temp0 = 1
						end if			
					end if
				next
				if temp0 == 0
					achievement.trigger = 5
				end if
			end if
		end if
	end if
end event


event ObjectStartup
	if options.vsMode == 0
		SetMusicTrack("CasinoNight.ogg", 0, 0xF564)
		SpeedUpMusic = CNZSetup_Function99
		SlowDownMusic = CNZSetup_Function100
	else
		SetMusicTrack("CasinoNight2.ogg", 0, 0x1767C)
		SpeedUpMusic = CNZSetup_Function101
		SlowDownMusic = CNZSetup_Function102
	end if
	SetPaletteEntry(0, 192, 0)
	animalType1 = TypeName[Flicky]
	animalType2 = TypeName[Becky]
	arrayPos0 = 0
	while arrayPos0 < 576
		temp0 = arrayPos0
		temp0 &= 63
		GetTableValue(stage[arrayPos0].deformationData2, temp0, CNZSetup_table38)
		arrayPos0++
	loop
	object[10].type = TypeName[CNZ Setup]
	object[10].priority = PRIORITY_ACTIVE
	object[10].value3 = 1
	object[10].value4 = 2
	object[10].value7 = 70
	if options.attractMode == 1
		switch stage.playerListPos
		case 0
			PlayerObject_ReplayTable = CNZSetup_table0
			PlayerObject_ReplaySize = 142
			PlayerObject_ReplayLength = 0x708
			break
		case 1
			PlayerObject_ReplayTable = CNZSetup_table1
			PlayerObject_ReplaySize = 522
			PlayerObject_ReplayLength = 0x708
			break
		case 2
			PlayerObject_ReplayTable = CNZSetup_table2
			PlayerObject_ReplaySize = 178
			PlayerObject_ReplayLength = 0x708
			break
		end switch
		CallFunction(PlayerObject_InitReplay)
	end if
	switch stage.actNum
		case 0
		case 1
			//LoadPalette("CNZAct1Pal.act", 0, 128, 128, 256)
			break
		case 2
			LoadPalette("CNZAct2Pal.act", 0, 128, 128, 256)
			SetMusicTrack("CasinoNightAct2.ogg", 0, 88273)
			break
	end switch
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
